package Snake;

import java.util.ArrayList;

import Food.Food;
import GameSet.SnakeFrame;

public class Snake {
	static boolean isRun;
	public static ArrayList<Food> body;
	public Food food;
	int direction;
	static int score;
	static int nowScore;
	static int status;
	static int speed;
	// repaint frequency
	public static final int SLOW = 500;
	public static final int SLOW_SCORE = 10;
	public static final int MID = 300;
	public static final int MID_SCORE = 20;
	public static final int FAST = 100;
	public static final int FAST_SCORE = 30;
	public static final int RUNNING = 1;
	public static final int PAUSED = 2;
	public static final int GAMEOVER = 3;
	public static final int LEFT = 1;
	public static final int UP = 2;
	public static final int RIGHT = 3;
	public static final int DOWN = 4;

	public Snake() {
		speed = Snake.SLOW;
		nowScore = SLOW_SCORE;
		score = 0;
		isRun = false;
		status = Snake.PAUSED;
		direction = Snake.RIGHT;
		body = new ArrayList<Food>();
		body.add(new Food(60, 20));
		body.add(new Food(40, 20));
		body.add(new Food(20, 20));
		makeFood();
	}
	
	public static void setSpeed(int sp) {
		speed = sp;
	}
	
	public static int getSpeed() {
		return speed;
	}
	
	public static void setStatus(int statu) {
		status = statu;
	}
	
	public static int getStatus() {
		return status;
	}
	
	public static void setNowScore(int now) {
		nowScore = now;
	}
	
	public static int getScore() {
		return score;
	}

	public static void setIsRun(boolean Run){
		isRun = Run;
	}
	
	public static boolean getIsRun(){
		return isRun;
	}
	
	private boolean isEaten() {
		Food head = body.get(0);
		if (direction == Snake.RIGHT && ((head.x + Food.W) == food.x)
				&& head.y == food.y) {
			return true;
		}
		if (direction == Snake.LEFT && ((head.x - Food.W) == food.x)
				&& head.y == food.y) {
			return true;
		}
		if (direction == Snake.UP && ((head.y - Food.H) == food.y)
				&& head.x == food.x) {
			return true;
		}
		if (direction == Snake.DOWN && ((head.y + Food.H) == food.y)
				&& head.x == food.x) {
			return true;
		} else {
			return false;
		}
	}

	private boolean isCollision() {
		Food head = body.get(0);
		if (direction == Snake.RIGHT
				&& head.x == SnakeFrame.PANEL_WEIGHT - Food.W) {
			return true;
		}
		if (direction == Snake.UP && head.y == 0) {
			return true;
		}
		if (direction == Snake.LEFT && head.x == 0) {
			return true;
		}
		if (direction == Snake.DOWN
				&& head.y == SnakeFrame.PANEL_HEIGHT - Food.H) {
			return true;
		}
		Food temp = null;
		int i = 0;
		for (i = 3; i < body.size(); i++) {
			temp = body.get(i);
			if (temp.x == food.x && temp.y == food.y) {
				break;
			}
		}
		if (i < body.size()) {
			return true;
		} else {
			return false;
		}
	}

	public void changeDirection(int newDir){
		if (direction % 2 != newDir % 2) {
			direction = newDir;
		}
	}
	
	public void move() {
		if (isEaten()) {
			body.add(0, food);
			score += nowScore;
			makeFood();
		} else if (isCollision()) {
			isRun = false;
			status = Snake.GAMEOVER;
		} else if (isRun) {
			Food food = body.get(0);
			int X = food.x;
			int Y = food.y;
			switch (direction) {
			case LEFT:
				X -= Food.W;
				break;
			case UP:
				Y -= Food.H;
				break;
			case RIGHT:
				X += Food.W;
				break;
			case DOWN:
				Y += Food.H;
				break;
			default:
				break;
			}
			body.add(0, new Food(X, Y));
			body.remove(body.size() - 1);
		}
	}
	
	public void makeFood() {
		food = new Food(0, 0);
		boolean isInBody = true;
		int x = 0, y = 0;
		int X = 0, Y = 0;
		int i = 0;
		while (isInBody) {
			x = (int) Math.random() * 15;
			y = (int) Math.random() * 20;
			X = x * Food.W;
			Y = y * Food.H;
			for (i = 0; i < Snake.body.size(); i++) {
				if (X == Snake.body.get(i).x && Y == Snake.body.get(i).y) {
					break;
				}
			}
			if (i < Snake.body.size()) {
				isInBody = true;
			} else {
				isInBody = false;
			}
		}
		food = new Food(X, Y);
	}
	
}
